package com.example.news;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.news.sql.MYSQL;

public class WangjimiamActivity extends AppCompatActivity implements View.OnClickListener {

    private Toolbar wjmm_toolbar;
    private EditText wjmm_edit_yonghumingaa;
    private EditText wjmm_edit_mimaaa;
    private EditText edit_chongfumima;
    private Button wjmm_btn_zhuce;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wangjimiam);
        initView();
    }

    private void initView() {
        wjmm_toolbar = (Toolbar) findViewById(R.id.wjmm_toolbar);
        setSupportActionBar(wjmm_toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        wjmm_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        wjmm_edit_yonghumingaa = (EditText) findViewById(R.id.wjmm_edit_yonghumingaa);
        wjmm_edit_mimaaa = (EditText) findViewById(R.id.wjmm_edit_mimaaa);
        edit_chongfumima = (EditText) findViewById(R.id.wjmm_edit_chongfumima);
        wjmm_btn_zhuce = (Button) findViewById(R.id.wjmm_btn_zhuce);

        wjmm_btn_zhuce.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.wjmm_btn_zhuce:
                submit();
                String yonghumingaa = wjmm_edit_yonghumingaa.getText().toString().trim();
                String mimaaa = wjmm_edit_mimaaa.getText().toString().trim();
                String chongfumima = edit_chongfumima.getText().toString().trim();
                if (mimaaa.equals(chongfumima)){
                    MYSQL mysql=new MYSQL(WangjimiamActivity.this);
                    ContentValues contentValues=new ContentValues();
                    contentValues.put("password",mimaaa);
                    SQLiteDatabase sqLiteDatabase=mysql.getWritableDatabase();
              int  a=sqLiteDatabase.update("information",contentValues,"name=?",new String[]{yonghumingaa});
              if (a>0){
                  finish();
                  startActivity(new Intent(WangjimiamActivity.this,LoginAvtivity.class));
              }
                }else{
                    Toast.makeText(this, "两次密码不统一", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void submit() {
        // validate
        String yonghumingaa = wjmm_edit_yonghumingaa.getText().toString().trim();
        if (TextUtils.isEmpty(yonghumingaa)) {
            Toast.makeText(this, "用户名", Toast.LENGTH_SHORT).show();
            return;
        }

        String mimaaa = wjmm_edit_mimaaa.getText().toString().trim();
        if (TextUtils.isEmpty(mimaaa)) {
            Toast.makeText(this, "密码", Toast.LENGTH_SHORT).show();
            return;
        }

        String chongfumima = edit_chongfumima.getText().toString().trim();
        if (TextUtils.isEmpty(chongfumima)) {
            Toast.makeText(this, "重复密码", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something


    }
}
